import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.opengis.feature.simple.*;
import org.opengis.feature.type.*;

public class ShpAttributes implements Serializable {
	private static final long serialVersionUID = 6947403344262247581L;
	private List<Map> fieldsSource;
	
	public ShpAttributes(List<Map> fieldListSource) {
		this.fieldsSource = fieldListSource;
	}
	
	public Map<String, String> getFieldMapping(List<String> fieldsSourceNoGeometry) {
		Map<String, String> fieldMapping = new HashMap<>();
		int cnts = 1;
		for (String fieldSource : fieldsSourceNoGeometry) {
			if (fieldSource.length() < 10) {
				fieldMapping.put(fieldSource, fieldSource);
			} else {//需要处理两种情况：一是自身超过10个字符需要截取，另一个是截取之后是否有重复的
				String fieldNew = fieldSource.substring(0, 9) + "_";
				if (fieldMapping.containsKey(fieldNew)) {
					fieldNew = fieldSource.substring(0, 8) + "_" + cnts;
					fieldMapping.put(fieldNew, fieldSource);
					cnts++;
				} else {
					fieldMapping.put(fieldNew, fieldSource);
				}
			}
		}
		return fieldMapping;
	}
	
	public Map<String, String> getFieldMapping(SimpleFeatureType typeSource) {
		Map<String, String> fieldMapping = new HashMap<>();
		String fieldGeometry = typeSource.getGeometryDescriptor().getName().toString();
		List<String> attrsSourceWithoutGeo = new ArrayList<>();
		for (AttributeDescriptor attrSource : typeSource.getAttributeDescriptors()) {
			if (attrSource.getName().toString().equalsIgnoreCase(fieldGeometry)) {
				continue;
			}
			attrsSourceWithoutGeo.add(attrSource.getName().toString());
		}
		return getFieldMapping(attrsSourceWithoutGeo);
	}
}
